home *** CD-ROM | disk | FTP | other *** search
- /*
- MOD 07-12-87 MAZ - RIFF input routines
- */
- #include <FileMgr.h>
- #include "MAZlib.h"
- #include "RIFF.h"
-
- extern bool init_io();
- extern long init_read();
-
- /* stdfile externs */
- extern long filter_type;
- extern long filter_creator;
- extern short global_volrefnum;
- extern bool input_file_name();
-
- /* information as read in */
- unsigned char *body[4];
- short nrows;
- short ncols;
- short storage_type;
- short nsamples;
- short dpi;
- bool uncompressed;
- unsigned char *transfer_table;
-
- /* from RIFFcompress.C */
- extern bool COMPinput();
-
- /* open a new picture */
- /* note: if the nsamples of the picture is not equal to the nsamples desired */
- /* conversion occurs */
- short open_RIFF(filename, desired_nsamples)
- char *filename; /* returned */
- short desired_nsamples; /* range: 2 to 256 */
- {
- bool b;
- short flat, i, j, nchunks;
- long lsize;
- short *huffbuff;
- ioParam readparm;
- RIFF_header Rheader;
-
- /* get file for input */
- filter_creator = 0;
- filter_type = 'RIFF'; /* allow riff files only */
- b = input_file_name("Open Picture:", filename);
- if (!b)
- return(-1); /* user cancelled */
- /* open the file */
- CtoPstr(filename);
- readparm.ioNamePtr = (StringPtr)filename;
- readparm.ioVersNum = 0;
- readparm.ioVRefNum = global_volrefnum;
- readparm.ioPermssn = fsRdPerm;
- readparm.ioMisc = null;
- PBOpen(&readparm, false);
- PtoCstr(filename);
- /* get out if the open failed (this should not happen) */
- if (readparm.ioResult != 0)
- return(2); /* coudn't open file */
- /* read the pattern record from the file */
- if (!init_io(2048L))
- goto no_mem2;
- init_read(&readparm, 0L);
- getbytes(&readparm, &Rheader, (long)sizeof(RIFF_header));
- nrows = Rheader.nrows;
- ncols = Rheader.ncols;
- nsamples = Rheader.nsamples;
- storage_type = Rheader.storage_type;
- switch (storage_type)
- {
- case st_gray:
- case st_vlt:
- nchunks = 1;
- break;
- case st_rgb:
- case st_hsv:
- case st_cmy:
- nchunks = 3;
- break;
- case st_cmyk:
- nchunks = 4;
- break;
- }
- /* read in resolution of picture */
- dpi = Rheader.resolution;
- if (dpi == 0)
- dpi = 72; /* for old files... */
- /* set the uncompressed flag */
- uncompressed = (Rheader.flags&hf_uncompressed) != 0;
- /* set the transfer function */
- transfer_table = (unsigned char *)NewPtr(nsamples);
- if (transfer_table == null)
- goto no_mem2;
- if (Rheader.flags&hf_transfer)
- getbytes(&readparm, transfer_table, (long)nsamples);
- else
- {
- /* assume normalized transfer */
- for (i = 0; i < nsamples; i++)
- transfer_table[i] = i;
- }
- /* convert transfer table for number of samples desired */
- if (desired_nsamples != nsamples)
- {
- for (i = 0; i < nsamples; i++)
- {
- j = transfer_table[i];
- j = ((long)desired_nsamples*(long)j + nsamples/2) / nsamples;
- transfer_table[i] = j;
- }
- }
- /* evaluate the size of one chunk */
- lsize = (long)nrows * (long)ncols;
- /* allocate all chunks at once */
- body[0] = (unsigned char *)NewPtr(lsize * (long)nchunks);
- if (body[0] == null)
- goto no_mem1;
- /* update all other chunk pointers */
- for (i = 1; i < nchunks; i++)
- body[i] = body[i-1] + lsize;
- /* determine the size of a flat scan line (largest record coming in from data) */
- flat = (4 + ncols) & ~1;
- /* allocate the decoding buffer plus some slop */
- huffbuff = (short *)NewPtr((long)flat + 16);
- if (huffbuff == null)
- {
- DisposPtr(body[0]);
- body[0] = null;
- no_mem1:
- DisposPtr(transfer_table);
- no_mem2:
- term_io();
- /* close the file */
- PBClose(&readparm, false);
- return(1); /* not enough memory available */
- }
- /* load up the chunks now */
- for (i = 0; i < nchunks; i++)
- {
- /* position to the header */
- pos_read(&readparm, Rheader.chunks[i]);
- /* do compressed or uncompressed readin */
- if (uncompressed)
- getbytes(&readparm, body[i], lsize);
- else
- COMPinput(&readparm, huffbuff, i);
- }
- /* free the decode buffer */
- DisposPtr(huffbuff);
- /* free I/O buffer */
- term_io();
- clodraw:
- /* close the file */
- PBClose(&readparm, false);
- return(0); /* file opened successfully */
- }
-